import axios from "axios";
import qs from 'qs';

// 创建一个axios对象
let http = axios.create({
  // 根路径
  baseURL: 'http://localhost:8000/',
  // 连接超时时间
  timeout: 1000000000,
  // 是否携带凭证
  // withCredentials: true
});

http.interceptors.request.use(config => {
  // 请求方式是post
  if (config.method == "post" || config.method == "POST") {
    config.data = qs.stringify(config.data);
    config.headers['content-Type'] = 'application/x-www-form-urlencoded';
  }
  //获取token
  let token = sessionStorage.getItem('token');
  //请求携带token值
  if (token) {
    token = qs.parse(token)
    config.headers.Authorization = token.token_type + token.access_token
  }
  return config;
});

http.interceptors.response.use(response => {
  return response.data;
});

export default http

